package thread;

public class UnsafeAccount implements Runnable{
    private Account account=new Account("私人账户",600);
    private int money;
    public UnsafeAccount(int money){
        this.money=money;
    }
    //模拟取钱操作
    @Override
    public void run() {
        synchronized (this){
            if (account.money-money<0){
                System.out.println(Thread.currentThread().getName()+"不能取钱！当前还剩金额"+account.getMoney());
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.setMoney(account.getMoney()-money);
            System.out.println(Thread.currentThread().getName()+"取走了"+money+ "元，当前还剩金额为:"+account.getMoney());
        }
    }

    public static void main(String[] args) {
        UnsafeAccount unsafeAccount=new UnsafeAccount(500);
        new Thread(unsafeAccount,"men").start();
        new Thread(unsafeAccount,"women").start();
    }
}
class Account{
    String name;
    int money;

    public Account(String name, int money) {
        this.name = name;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}